[% setvar title Extensible Meta-Object Protocol %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Extensible Meta-Object Protocol</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Tony Olekshy &lt;<a href='mailto:olekshy@avrasoft.com'>olekshy@avrasoft.com</a>&gt;
  Date: 11 Aug 2000
  Last Modified: 1 Oct 2000
  Mailing List: <a href='mailto:perl6-language-objects@perl.org'>perl6-language-objects@perl.org</a>
  Number: 92
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>In the name of keeping Perl Perl and easy easy, Perl 6 should
maintain the current syntax and semantics for blessed objects.</p>
<p>Perl 6 should not <i>require</i> the use of an alternative meta-object
protocol, nor should it <i>prevent</i> the development of other
protocols (such as those which implement a stricter protocol).</p>
<p>However, Perl 6 should provide hooks into object protocol mechanisms
that would enhance the development of modules that implement other
meta-object protocols.</p>
<p>This RFC considers the matter of an extensible method search
protocol, which allows things like limited visibility ivars and
methods, and breadth-first multiple inheritance, to be efficiently
implemented as modules rather than as core Perl behaviour that would
be forced on all.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Perl's current object-oriented stuff works well for certain classes
of problems.  It doesn't interfere with Perl programs that don't
want to be object-oriented at all.  It provides a foundation upon
which more complex object protocols can be implemented.</p>
<p>However, since all method names are visible in all contexts (they
are effectively &quot;public&quot;), it is difficult to write frameworks
containing abstract base classes that are designed to be heavily
inherited from, because internal (&quot;private&quot;) methods can't be added
to the base classes without impacting all users of the framework.
Similar problems arise with instance variables.</p>
<p>At Avra we address these problems by using a module that allows us
to write publically visible framework base classes like this:</p>
<pre>    package MyClass; use Prothos::Class ISA =&gt; ParentClass;

    ivar Foo =&gt; Public,    Write   =&gt; Private;
    ivar Bar =&gt; Private,   Default =&gt; &quot;Hello, World&quot;;
    ivar Baz =&gt; Protected, Default =&gt; {};

    method HelloWorld =&gt; Public, sub
    {
	my ($I, %A) = @_;

	$I-&gt;Baz(Name =&gt; $A{Name});

	print $I-&gt;Baz(&quot;Name&quot;), &quot; says \&quot;&quot;, $I-&gt;Bar, &quot;,\&quot; to $A{To}.\n&quot;;
	};

    method Cogitate =&gt; Private, sub
    {
	...
	};

    method OverrideMe =&gt; Protected, Bind =&gt; Virtual, sub
    {
	...
	};</pre>
<p>These classes play well with canonical Perl classes.  Only the
behaviour of the package is affected, Perl is not globally affected.</p>
<p>Our module constructs objects as blessed references to an integer
which is an index into a hidden generator-scoped array of all
extant objects in the class, which means there is no way to get to
an actual object's data unless one of the (generated) methods that
can see the hidden array is used.</p>
<p>The ivar declarations are converted into generated method subs that
access the hidden ivar data.  Ivars are internally qualified by
package name, so shadowed ivars are correctly handled.</p>
<p>Methods' subs are <i>saved</i> in a hidden internal hash, and generated
subs are installed in the package namespace to fetch and invoke the
correct method closure from the hidden internal hash, using rules
that depend on the declared visiblity of the method.</p>
<p>This works because all the generated subs of the same name are
<i>the same</i>, independent of package, so no matter which one is
found by Perl, the caller() context and visibility rules are used
to determine the actual closure to invoke.  The generated methods
basically look like this (where $_class and $method are the names
of the class and method):</p>
<pre>    $_stash-&gt;{$method} = \&amp;{&quot;${_class}::$method&quot;} = sub
    {
	$_ = $_cache-&gt;{$method}-&gt;{scalar caller} and goto $_;

	goto \&amp;{ &amp;{$_stash-&gt;{_First_}}($method, @_) };
	};</pre>
<p>The run-time cost of setting all this up and doing the cache
initializations and lookups is acceptable to us.  However, our
<i>method invocations now take up to twice as long</i>, even with
caching, because there are two &quot;function calls&quot;, one for the
<code>sub</code> and one for the <code>goto</code>.</p>
<p>This problem can be solved by allowing Perl's method search
mechanism to be overridden by an installed subroutine, on a
per-package basis.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>When Perl performs a method lookup it starts in the package of
the given object and searches up the tree of @ISA packages.</p>
<p>Perl should be modified so that if <code>$ISA::Search</code> (or equivalent)
is defined for a package, then when a method is being looked up for
an object in that package, the code ref given by that value is
invoked instead of performing the default search.</p>
<p>The code ref should be passed the class name, method name, and the
value of <code>scalar caller</code> (since method visibility algorithms
typically depend on the name of the invoking context).  The code ref
should be written to return the package-qualified sub name to be
invoked.</p>
<p>This return value should be memoized by Perl, that is, given the
same class name, method name, and <code>scalar caller</code>, the search code
ref should not be invoked at all.  Asymptotically, this gets us back
down to one &quot;function call&quot; per method invocation.</p>
<p>In general, it should be possible to override the ISA search
mechanism, the -&gt; method invoker, and the <code>bless</code> operator,
on a per-class basis.</p>
<a name='TERMINOLOGY'></a><h1>TERMINOLOGY</h1>
<p>A &quot;meta-object protocol&quot; is the set of rules that determines the
behaviour of objects.  For more information see section 1.6 in the
OO FAQ at <a href='http://www.cyberdyne-object-sys.com/oofaq2' target='_blank'>www.cyberdyne-object-sys.com</a> , from which:
&quot;In CLOS terminology, an introspective protocol provides a read
only capability (e.g. what is this object's class, give info on
this class, etc.) and an intercessory protocol provides a write
capability which allows system modification (e.g. add the
following method or instance to this class, perform inheritance
this way, etc.).  Because inheritance can be used to perform
differential changes, intercessory protocols allow users to not
only define new frameworks but to specialize existing system
frameworks differentially without affecting them and their
extant objects. Thus, many frameworks can interoperate together
simultaneously.&quot;</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 28: Perl should stay Perl.</p>
<p>OO FAQ: <a href='http://www.cyberdyne-object-sys.com/oofaq2' target='_blank'>www.cyberdyne-object-sys.com</a></p>
</div>
